package com.example.service.impl;

import com.example.domain.dto.SysUserDto;
import com.example.domain.pojo.SysPermission;
import com.example.domain.pojo.SysRole;
import com.example.domain.vo.SysPermissionVo;
import com.example.mapper.IPermissionMapper;
import com.example.mapper.IUserMapper;
import com.example.service.ISysPermissionService;
import com.example.utils.SecurityUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ljs
 * @date 2025/7/26
 */
@Service
public class SysPermissionImpl implements ISysPermissionService {
    @Autowired
    private SecurityUtil securityUtil;

    @Autowired
    private IPermissionMapper permissionMapper;

    //获取权限列表
    @Override
    public List<SysPermissionVo> getPermission() {
        //获取roleid
        SysUserDto sysUserDto = securityUtil.getSysUserDto();
        List<SysRole> roles = sysUserDto.getRoles();
        List<Integer> roleIds = roles.stream().map(SysRole::getRoleId).collect(Collectors.toList());
        //根据roleid获取权限列表
        List<SysPermission> sysPermissions = permissionMapper.selectPersByRoleIds(roleIds);
        //List<String> perms = sysPermissions.stream().map(SysPermission::getMenuName).collect(Collectors.toList());

        return getParentPer(sysPermissions);

    }

    //获取父级
    public List<SysPermissionVo> getParentPer(List<SysPermission> sysPermissions) {
        List<SysPermissionVo> sysPermissionVos = new ArrayList<>();
        //获取父项per
        List<SysPermission> parentPer =
                sysPermissions
                        .stream().
                        filter(item -> item.getParentId() == 0)
                        .collect(Collectors.toList());

        for (SysPermission sysPermission : parentPer) {
            SysPermissionVo sysPermissionVo = new SysPermissionVo();
            BeanUtils.copyProperties(sysPermission, sysPermissionVo);
            sysPermissionVos.add(sysPermissionVo);
        }
        //第一级子项
        List<SysPermissionVo> childPers = new ArrayList<>();
        for (SysPermission sysPermission : parentPer) {
            List<SysPermissionVo> childPer = getChildPer(sysPermission.getPerId(), sysPermissions);
            childPers.addAll(childPer);
        }

        for (SysPermissionVo sysPermissionVo : sysPermissionVos) {
            List<SysPermissionVo> collect = childPers.stream().filter(item -> item.getParentId().equals(sysPermissionVo.getPerId()))
                    .collect(Collectors.toList());
            sysPermissionVo.setChildren(collect);
        }
        return sysPermissionVos;
    }

    //获取子级
    public List<SysPermissionVo> getChildPer(Long parentPerId , List<SysPermission> all) {

        List<SysPermissionVo> childPerVo = new ArrayList<>();
        List<SysPermission> childPerms =
                all.stream().
                    filter(item -> item.getParentId().equals(parentPerId) )
                    .collect(Collectors.toList());
        for (SysPermission childPerm : childPerms) {
            SysPermissionVo sysPermissionVo = new SysPermissionVo();
            BeanUtils.copyProperties(childPerm, sysPermissionVo);
            childPerVo.add(sysPermissionVo);
        }

        List<SysPermissionVo> childPers = new ArrayList<>();
        for (SysPermission childPerm : childPerms) {
            List<SysPermissionVo> childPer = getChildPer(childPerm.getPerId(), all);
            childPers.addAll(childPer);
        }

        //设子级
        for (SysPermissionVo sysPermissionVo : childPerVo) {
            List<SysPermissionVo> collect =
                    childPers.stream().filter(item -> item.getParentId() == sysPermissionVo.getPerId())
                            .collect(Collectors.toList());
            sysPermissionVo.setChildren(collect);

        }

        return childPerVo;

    }
}
